package ui.troopmanager.mooresr.us;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import troopmanager.mooresr.us.DataManager;
import troopmanager.mooresr.us.Event;
import troopmanager.mooresr.us.EventParticipant;
import troopmanager.mooresr.us.Person;
import troopmanager.mooresr.us.SortedListModel;
import troopmanager.mooresr.us.Vehicle;
import troopmanager.mooresr.us.VehicleDriver;

public class DriversDialog extends JDialog {

	private static final long serialVersionUID = 3943554489637055257L;
	
	private JPanel centerPanel;
	private JPanel topPanel;
	private JList leftList;
	private SortedListModel<Person> leftListModel;
	private JScrollPane leftScrollPane;
	private JPanel buttonPanel;
	private JButton addButton;
	private JButton removeButton;
	private JList rightList;
	private SortedListModel<Vehicle> rightListModel;
	private JScrollPane rightScrollPane;
	private JPanel bottomPanel;
	private JList bottomList;
	private SortedListModel<VehicleDriver> bottomListModel;
	private JScrollPane bottomScrollPane;
	private JLabel seatsLabel;
	private JLabel participantsLabel;
	private JPanel buttonPanel2;
	private JButton okButton;
	private JButton cancelButton;
	
	private Integer eventId;
	
	private Integer seats;
	private Integer participants;
	

	public DriversDialog() {
		seats = 0;
		participants = 0;
		initComponents();
	}
	
	private void initComponents() {
		BorderLayout layout = new BorderLayout();
	
		setTitle("Event Drivers");
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setBackground(Color.white);
		setForeground(Color.black);

		setLayout(layout);
		
		centerPanel = new JPanel();
		topPanel = new JPanel();
		leftListModel = new SortedListModel<Person>();
		leftList = new JList(leftListModel);
		leftList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		leftList.setPrototypeCellValue(new String("mmmmmmmmmmmmm"));
		leftScrollPane = new JScrollPane();
		leftScrollPane.setViewportView(leftList);
		buttonPanel = new JPanel();
		addButton = new JButton("Add");
		addButton.setEnabled(false);
		removeButton = new JButton("Remove");
		removeButton.setEnabled(false);
		rightListModel = new SortedListModel<Vehicle>();
		rightList = new JList(rightListModel);
		rightList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		rightList.setPrototypeCellValue(new String("mmmmmmmmmmmmmmmmmmmmmmmmmmmm"));
		rightScrollPane = new JScrollPane();
		rightScrollPane.setViewportView(rightList);
		bottomPanel = new JPanel();
		bottomListModel = new SortedListModel<VehicleDriver>();
		bottomList = new JList(bottomListModel);
		bottomList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		bottomList.setPrototypeCellValue(new String("mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"));
		bottomScrollPane = new JScrollPane();
		bottomScrollPane.setViewportView(bottomList);
		buttonPanel2 = new JPanel();
		okButton = new JButton("OK");
		cancelButton = new JButton("Cancel");
		
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				okButtonCallback(event);
			}
		});
		
		cancelButton.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent event) {
				cancelButtonCallback(event);
			}
		});
		
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				addButtonCallback(event);
			}
		});
		
		removeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				removeButtonCallback(event);
			}
		});
		
		leftList.addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				if (leftList.isSelectionEmpty() || rightList.isSelectionEmpty()) {
					addButton.setEnabled(false);
				} else {
					addButton.setEnabled(true);
				}
			}			
		});
		
		rightList.addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				if (leftList.isSelectionEmpty() || rightList.isSelectionEmpty()) {
					addButton.setEnabled(false);
				} else {
					addButton.setEnabled(true);
				}
			}			
		});
		
		bottomList.addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				if (bottomList.isSelectionEmpty()) {
					removeButton.setEnabled(false);
				} else {
					removeButton.setEnabled(true);
				}
			}			
		});
		
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
		buttonPanel.add(addButton);
		buttonPanel.add(removeButton);
		
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.LINE_AXIS));
		topPanel.add(leftScrollPane);
		topPanel.add(buttonPanel);
		topPanel.add(rightScrollPane);
		
		buttonPanel2.setLayout(new BoxLayout(buttonPanel2, BoxLayout.LINE_AXIS));
		buttonPanel2.add(okButton);
		buttonPanel2.add(cancelButton);
		
		seatsLabel = new JLabel("Total seats:");
		participantsLabel = new JLabel("Total participants:");
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.PAGE_AXIS));
		bottomPanel.add(bottomScrollPane);
		bottomPanel.add(seatsLabel);
		bottomPanel.add(participantsLabel);
		
		centerPanel.add(topPanel);
		centerPanel.add(bottomPanel);
		
		add(centerPanel, BorderLayout.CENTER);
		add(buttonPanel2, BorderLayout.SOUTH);
		
		setSize(700, 400);
		setModal(true);
		

		
	}
	
	private void okButtonCallback(ActionEvent event) {
		LinkedList<VehicleDriver> drivers = new LinkedList<VehicleDriver>();
		
		for (int i=0; i<bottomListModel.size(); i++) {
			VehicleDriver d = (VehicleDriver) bottomListModel.get(i);
			drivers.add(d);
		}
		
		DataManager.getInstance().setEventDrivers(eventId, drivers);
		
		setVisible(false);
	}
	
	private void cancelButtonCallback(ActionEvent event) {

		setVisible(false);

	}
	
	private void addButtonCallback(ActionEvent event) {
		Person driver = (Person) leftList.getSelectedValue();
		Vehicle vehicle = (Vehicle) rightList.getSelectedValue();
		
		VehicleDriver vehicleDriver = new VehicleDriver(driver, vehicle);
		
		
		bottomListModel.addElement(vehicleDriver);
		leftListModel.removeElement(driver);
		rightListModel.removeElement(vehicle);
		
		updateTotalSeats();
		
	}
	
	private void removeButtonCallback(ActionEvent event) {
		VehicleDriver vehicleDriver = (VehicleDriver) bottomList.getSelectedValue();
		
		Person driver = DataManager.getInstance().getPerson(vehicleDriver.getPersonId());
		Vehicle vehicle = DataManager.getInstance().getVehicle(vehicleDriver.getVehicleId());
		
		bottomListModel.removeElement(vehicleDriver);
		leftListModel.addElement(driver);
		rightListModel.addElement(vehicle);
		
		updateTotalSeats();
		
	}
	
	public void loadData(Event e) {
		
		// The top left list gets all eligible drivers (adult, participant, not already driving)
		LinkedList<Person> eligibleDrivers = (LinkedList<Person>) DataManager.getInstance().getEligibleDrivers(e);
		leftListModel.clear();
		for (Person p : eligibleDrivers) {
			leftListModel.addElement(p);
		}
		
		// The top right list gets all eligible vehicles (not already being driven)
		LinkedList<Vehicle> availableVehicles = (LinkedList<Vehicle>) DataManager.getInstance().getAvailableVehicles(e);
		rightListModel.clear();
		for (Vehicle v : availableVehicles) {
			rightListModel.addElement(v);
		}
		
		// The bottom list gets all driver/vehicle pairs for this event
		LinkedList<VehicleDriver> drivers = (LinkedList<VehicleDriver>) DataManager.getInstance().getAllVehicleDrivers(e);
		bottomListModel.clear();
		for (VehicleDriver d : drivers) {
			bottomListModel.addElement(d);
		}
		
		LinkedList<EventParticipant> allParticipants = (LinkedList<EventParticipant>) DataManager.getInstance().getEventParticipants(e);
		participants = allParticipants.size();
		participantsLabel.setText("Total Participants: " + participants.toString());
		
		updateTotalSeats();
		eventId = e.getEventId();

		bottomList.setPreferredSize(new Dimension(100,100));
		
	}
	
	private void updateTotalSeats()
	{
		seats = 0;
		
		int numDrivers = bottomListModel.getSize();
		
		for (int i=0; i<numDrivers; i++) {
			VehicleDriver d = (VehicleDriver) bottomListModel.getElementAt(i);
			seats += d.getSeatbelts();
		}
		
		seatsLabel.setText("Total seats: " + seats.toString());
		
	}

}
